package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {
    @Resource
    private SearchMasterMessageMapper searchMasterMessageMapper;
//    @Autowired
//    private RedisService redisService;
    @Resource
    private SearchMasterMsgMapper msgMapper;

    @Override
    public JsonResult publish(SearchMasterMessage searchMasterMessage) {
        //获取发布id

//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String token = request.getHeader("token");
//        LoginInfo info = redisService.getKeyObjectValue(token, LoginInfo.class);
        Long userId = LoginInfoUtil.getUserId();
        searchMasterMessage.setUserId(userId);
        searchMasterMessageMapper.insertSelective(searchMasterMessage);


        return JsonResult.createSuccess();
    }

    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery messageQuery) {
        PageHelper.startPage(messageQuery.getPage(),messageQuery.getSize());
        List<SearchMasterMessage> page = searchMasterMessageMapper.findPage(messageQuery);
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(page);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public JsonResult shenghe(SearchMasterMessage message) {
        //查询一把数据库,如果message的状态不是0 ,就返回"你的技术还可以,但是还不够" 扩展
        SearchMasterMessage searchMasterMessage1 = searchMasterMessageMapper.selectByPrimaryKey(message.getId());
        if(searchMasterMessage1.getState()!=0){
            throw new BusinessException("你的技术还可以,但是还不够");
        }
        //如果state为-1;
        if(message.getState()==-1){
            SearchMasterMessage searchMasterMessage = new SearchMasterMessage();
            searchMasterMessage.setState(message.getState());
            searchMasterMessage.setId(message.getId());
            //修改对象;
            searchMasterMessageMapper.updateByPrimaryKeySelective(searchMasterMessage);
            return JsonResult.createSuccess("驳回成功");
        }
        //审核通过就进入支付表
        //获取数据
        SearchMasterMessage searchMasterMessage = searchMasterMessageMapper.selectByPrimaryKey(message.getId());
        //修改审核表
        searchMasterMessage.setState(message.getState());
        searchMasterMessageMapper.updateByPrimaryKeySelective(searchMasterMessage);
        //烤呗进入订单表
        SearchMasterMsg searchMasterMsg = BeanUtil.copyProperties(searchMasterMessage, SearchMasterMsg.class);
        searchMasterMsg.setId(null);
        searchMasterMsg.setState(0);
        searchMasterMsg.setNote(message.getNote());
        msgMapper.insertSelective(searchMasterMsg);
        return JsonResult.createSuccess("审核成功");
    }
}
